<template>
	<view>
		<button type="default" @click="callNoLock">紧急呼叫（不拉权限）</button>
		<text>{{phonetype}}</text>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phonetype: -1
			}
		},
		onHide() {
			this.watch()
		},
		watch: {
			// phonetype:this.watch();
		},
		methods: {
			callNoLock() {
				// 导入Activity、Intent类  
				var Intent = plus.android.importClass("android.content.Intent");
				var Uri = plus.android.importClass("android.net.Uri");
				// 获取主Activity对象的实例  
				var main = plus.android.runtimeMainActivity();
				// 创建Intent  
				var uri = Uri.parse("tel:10086"); // 这里可修改电话号码  
				var call = new Intent("android.intent.action.CALL", uri);
				// 调用startActivity方法拨打电话  
				main.startActivity(call);
				this.phonetype = 3
				console.log()
			},
			watch() {
				console.log(this.phonetype)
				console.log('开始监听')
				let maintest = plus.android.runtimeMainActivity();
				let Contexttest = plus.android.importClass("android.content.Context");
				let telephonyManager = plus.android.importClass("android.telephony.TelephonyManager");
				let telManager = plus.android.runtimeMainActivity().getSystemService(Contexttest.TELEPHONY_SERVICE);
				let receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
					onReceive: function(Contexttest, intent) {
						console.log(this.phonetype);
						plus.android.importClass(intent);
						console.log(intent.getAction());
						// let telephonyManager = plus.android.importClass("android.telephony.TelephonyManager");
						// let telephonyManager=plus.android.runtimeMainActivity().getSystemService(Contexttest.TELEPHONY_SERVICE);
						this.phonetype = telManager.getCallState();
						let phoneNumber = intent.getStringExtra(telephonyManager.EXTRA_INCOMING_NUMBER);
						console.log("phonetype:" + this.phonetype); //电话状态 0->空闲状态 1->振铃状态 2->通话存在
						console.log(); //电话号
						switch (this.phonetype) {
							case 0:
								_this.getCallLog();
								console.log('aaa')
								break;
							case 1:
								console.log('bbb')
								break;
							case 2:
								console.log('ccc')
								break;
						}

						// _t.state.push({state:phonetype == 0?'空闲状态':phonetype == 1?'振铃状态':'通话存在', time:dateFormat('hh:mm:ss')})
					}
				});
				let IntentFilter = plus.android.importClass('android.content.IntentFilter');
				let filter = new IntentFilter();
				filter.addAction(telephonyManager.ACTION_PHONE_STATE_CHANGED);
				maintest.registerReceiver(receiver, filter);
			},
		}
	}
</script>

<style>

</style>
